#include<iostream>
#include "Lldi.h"

Lldi::Lldi()
  {
    this->p = this->q = 0;
    std::cout<<"\nLista l.l.d.i. a fost creata!\n";
  }

Lldi::Lldi(const Lldi& l)
  {
    this->p = l.p;
    this->q = l.q;
  }

void Lldi::listare()
  {
    Nod *r;
    r = this->p;
    if(r != 0)
      {
	std::cout<<"\nLista :\n\n";
	while(r != 0)
	  {
	    std::cout<<"Data:"<<r->data<<"\n";
	    r = r->urm;
	  }
	std::cout<<"\n";
      }
      else std::cout<<"\nLista este vida!\n";
  }

bool Lldi::operator==(const Lldi& l)
  {
    Nod *r1, *r2;
    r1 = this->p;
    r2 = l.p;
    while(r1 != this->q && r2 != l.q)
      {
        if(r1->data != r2->data) return false;
	r1 = r1->urm;
	r2 = r2->urm;
      }
    if((r1 == 0 && r2 != 0) || (r1 != 0 && r2 == 0)) return false;
    return true;
  }

bool Lldi::operator!=(const Lldi& l)
  {
    Nod *r1, *r2;
    r1 = this->p;
    r2 = l.p;
    while(r1 != this->q && r2 != l.q)
      {
        if(r1->data == r2->data) return false;
	r1 = r1->urm;
	r2 = r2->urm;
      }
    if((r1 == 0 && r2 != 0) || (r1 != 0 && r2 == 0)) return false;
    return true;
  }

void Lldi::operator+(Nod* d)
  {
    if(this->q == 0)
      {
        d->urm = d-> ant = 0;
	this->p = this->q = d;
      }
      else
        {
	  this->q->urm = d;
	  d->ant = this->q;
	  d->urm = 0;
	  this->q = d;
	}
  }

Lldi Lldi::concatenare(const Lldi& l)
  {
    Lldi aux;
    Nod *r;
    aux = (*this);
    r = l.p;
    aux.q->urm = r;
    r->ant = aux.q;
    aux.q = l.q;
    return aux;
  }

Lldi Lldi::reuniune(const Lldi& l)
  {
    Lldi aux;
    Nod *r1, *r2, *r3;
    bool b = false;
    aux.p = this->p;
    r1 = this->p->urm;
    r2 = l.p;
    r3 = aux.p;
    while(r2 != 0 && r1 != 0)
      {
        if(b == true)
	  {
	    r3->urm = r1;
	    r1->ant = r3;
	    r1 = r1->urm;
	    r3 = r3->urm;
	    b = false;
	  }
	  else
	    {
	      r3->urm = r2;
	      r2->ant = r3;
	      r2 = r2->urm;
	      r3 = r3->urm;
	      b = true;
	    }
      }
    aux.q = r3;
    if(r2 == 0)
      {
        aux.q->urm = r1;
	aux.q = this->q;
      }
      else
        {
	  aux.q->urm = r2;
	  aux.q = l.q;
	}
    return aux;
  }

Lldi Lldi::intersectie(Lldi& l)
  {
    Lldi aux;
    Nod *r1, *r2;
    r1 = this->p;std::cout<<" "<<r1;
    r2 = aux.p;
    while(r1 != 0)
      {
        if(l.cautaNod(r1->data) == true)
	    if(r2 == 0)
	      {
	        aux.p = r2 = r1;
		r2->ant = 0;
	      }
	      else
	        {
		  r2->urm = r1;
		  r1->ant = r2;
		  r2 = r2->urm;
		}
	r1 = r1->urm;
	r2->urm = 0;
      }
    aux.q = r2;
  }

bool Lldi::cautaNod(int d)
  {
    Nod *r;
    r = this->p;
    while(r != 0)
      {
        if(r->data == d) return true;
	r = r->urm;
      }
    return false;
  }